package homeWork;

import jdbc.DBUtil;

import java.sql.*;
import java.util.Scanner;

public class HomeWork06_2 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()) {

            Statement statement = connection.createStatement();
            Scanner scanner = new Scanner(System.in);
            System.out.println("用户名");
            String username = scanner.nextLine();
            System.out.println("密码");
            String password = scanner.nextLine();
            System.out.println("昵称");
            String nickname = scanner.nextLine();
            System.out.println("年龄");
            Integer age = scanner.nextInt();


            String sql1 = "SELECT username FROM userinfo WHERE username=?";
            PreparedStatement ps1 = connection.prepareStatement(sql1);
            ps1.setString(1,username);
            ResultSet r = ps1.executeQuery();
            if(r.next()){
                System.out.println("用户名已存在");
            }else{
                String sql2 = "INSTER INTO userinfo(username, password, nickname,age) VALUES(?,?,?,?) ";
                PreparedStatement ps2 = connection.prepareStatement(sql2);
                ps2.setString(1,username);
                ps2.setString(2,password);
                ps2.setString(3,nickname);
                ps2.setInt(4,age);
                int i = ps2.executeUpdate();
                if(i>0){
                    System.out.println("注册成功");
                }else {
                    System.out.println("注册失败");
                }

            }

        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}
/**1.介绍一下数据库的三范式，并举例说明?
 * 1.1字段是最小单位不能在拆分
 * 1.2在满足第一范式的情况下，表中字段必须依赖于全部主键而非部分主键
 *
 */

